package com.personal.portal.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.personal.portal.common.entity.Result;
import com.personal.portal.admin.entity.Project;
import com.personal.portal.admin.mapper.ProjectMapper;
import com.personal.portal.admin.service.ProjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目服务实现类
 * 
 * @author zhanghui
 * @version 1.0.0
 * @since 2025-11-10
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public Project queryById(Long id) {
        validateId(id);
        return projectMapper.selectOneById(id);
    }

    @Override
    public Project queryByEntity(Project project) {
        QueryWrapper queryWrapper = buildQueryWrapper(project);
        return projectMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    public List<Project> queryList(Project project) {
        QueryWrapper queryWrapper = buildQueryWrapper(project);
        return projectMapper.selectListByQuery(queryWrapper);
    }

    @Override
    public Result.PageResult<Project> queryPage(int pageNum, int pageSize, Project project) {
        QueryWrapper queryWrapper = buildQueryWrapper(project);
        Page<Project> paginate = projectMapper.paginate(pageNum, pageSize, queryWrapper);
        return new Result.PageResult<>(paginate.getRecords(), paginate.getTotalRow(), pageNum, pageSize);
    }

    @Override
    public int insert(Project project) {
        validateProject(project);
        return projectMapper.insert(project);
    }

    @Override
    public int update(Project project) {
        if (project.getId() == null) {
            throw new IllegalArgumentException("项目ID不能为空");
        }
        validateId(project.getId());
//        validateProject(project);
        
        // 检查是否存在
        Project existing = projectMapper.selectOneById(project.getId());
        if (existing == null) {
            throw new IllegalArgumentException("项目信息不存在");
        }
        
        return projectMapper.update(project);
    }

    @Override
    public int deleteById(Long id) {
        validateId(id);
        
        // 检查是否存在
        Project existing = projectMapper.selectOneById(id);
        if (existing == null) {
            throw new IllegalArgumentException("项目信息不存在");
        }
        
        return projectMapper.deleteById(id);
    }

    @Override
    public List<Long> deleteBatch(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("ID列表不能为空");
        }

        List<Long> successIds = new ArrayList<>();
        List<Long> failedIds = new ArrayList<>();

        for (Long id : ids) {
            try {
                validateId(id);
                
                // 检查是否存在
                Project existing = projectMapper.selectOneById(id);
                if (existing == null) {
                    failedIds.add(id);
                    continue;
                }

                int result = projectMapper.deleteById(id);
                if (result > 0) {
                    successIds.add(id);
                } else {
                    failedIds.add(id);
                }
            } catch (Exception e) {
                failedIds.add(id);
            }
        }

        if (!successIds.isEmpty()) {
            return successIds;
        } else {
            throw new IllegalArgumentException("批量删除失败");
        }
    }

    /**
     * 验证项目信息
     */
    private void validateProject(Project project) {
        if (project == null) {
            throw new IllegalArgumentException("项目信息不能为空");
        }
        if (StringUtils.isBlank(project.getName())) {
            throw new IllegalArgumentException("项目名称不能为空");
        }
        if (StringUtils.isBlank(project.getDescription())) {
            throw new IllegalArgumentException("项目描述不能为空");
        }
    }

    /**
     * 验证ID
     */
    private void validateId(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("无效的项目ID");
        }
    }

    /**
     * 构建查询条件
     */
    private QueryWrapper buildQueryWrapper(Project project) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(Project::getId, project.getId(), project.getId() != null);
        queryWrapper.like(Project::getName, project.getName(), StringUtils.isNotEmpty(project.getName()));
        queryWrapper.eq(Project::getCategory, project.getCategory(), StringUtils.isNotEmpty(project.getCategory()));
        queryWrapper.like(Project::getDescription, project.getDescription(), StringUtils.isNotEmpty(project.getDescription()));
        queryWrapper.like(Project::getTechStack, project.getTechStack(), StringUtils.isNotEmpty(project.getTechStack()));
        queryWrapper.eq(Project::getProjectStatus, project.getProjectStatus(), StringUtils.isNotEmpty(project.getProjectStatus()));
        queryWrapper.eq(Project::getStatus, project.getStatus(), project.getStatus() != null);
        queryWrapper.ge(Project::getStartDate, project.getStartDate(), project.getStartDate() != null);
        queryWrapper.le(Project::getEndDate, project.getEndDate(), project.getEndDate() != null);

        queryWrapper.orderBy(Project::getSortOrder, true);
        queryWrapper.orderBy(Project::getId, true);
        return queryWrapper;
    }
}